<?php
    /**
    * View.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    require_once 'smarty/Smarty.class.php';
    
    /**
    * This view uses smarty and the default templates to display the module
    * information.
    * @package Atman
    */
    class AT_View_smarty extends AT_View
    {
        /**
        * The instance of the templating engine.
        * @var Smarty
        */
        private $smarty;
        
        /**
        * Sets up Smarty to be ready to display the template files.
        */
        public function __construct(AT_Module $module)
        {
            parent::__construct($module);
            
            $this->smarty = new Smarty;
            
            $this->smarty->template_dir =
                AT_ROOT.'/tpl/'.AT_TEMPLATE.'/templates';
                
            $this->smarty->compile_dir =
                AT_ROOT.'/tpl/'.AT_TEMPLATE.'/compiled';
        }
        
        /**
        * Displays the module's information using the correct template.
        * @return void
        */
        public function display()
        {
            $modName = $this->module->getModuleName();
            $modPath = AT_ROOT.'/mod/'.$modName.'/tpl';
            
            $session = AT_Session::singleton();
            
            $this->smarty->assign('modPath', $modPath);
            $this->smarty->assign('tplFile', $this->module->getTplFile());
            $this->smarty->assign('siteRoot', AT_SITE_ROOT);
            
            if ( $session->get('userID') != NULL )
            {
                $this->smarty->assign('loggedin', true);
                
                $user = new AT_User($session->get('userID'));
                $this->smarty->assign('user', $user);
            }
            
            $data = $this->module->getData();
            
            foreach ( $data as $var => $val )
            {
                $vars = $this->smarty->get_template_vars();
                if ( !array_key_exists($var, $vars) )
                {
                    $this->smarty->assign($var, $val);
                }
            }
            
            $template = $this->module->getPageTemplateFile() == NULL
                ? 'page.tpl'
                : $this->module->getPageTemplateFile();
            
            $this->smarty->display($template);
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
